package com.wolfcode.record.service.impl;

import com.wolfcode.common.utils.DateUtils;
import com.wolfcode.common.utils.LicensePlateUtil;
import com.wolfcode.common.utils.PhoneUtil;
import com.wolfcode.common.utils.SecurityUtils;
import com.wolfcode.record.domain.BusCustomerInformation;
import com.wolfcode.record.mapper.BusCustomerInformationMapper;
import com.wolfcode.record.service.IBusCustomerInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * 客户信息Service业务层处理
 *
 * @author wolfcode
 * @date 2022-12-08
 */
@Service
public class BusCustomerInformationServiceImpl implements IBusCustomerInformationService {
    @Autowired
    private BusCustomerInformationMapper busCustomerInformationMapper;

    /**
     * 查询客户信息
     *
     * @param id 客户信息主键
     * @return 客户信息
     */
    @Override
    public BusCustomerInformation selectBusCustomerInformationById(Long id) {
        return busCustomerInformationMapper.selectBusCustomerInformationById(id);
    }

    /**
     * 查询客户信息列表
     *
     * @param busCustomerInformation 客户信息
     * @return 客户信息
     */
    @Override
    public List<BusCustomerInformation> selectBusCustomerInformationList(BusCustomerInformation busCustomerInformation) {
        return busCustomerInformationMapper.selectBusCustomerInformationList(busCustomerInformation);
    }

    /**
     * 新增客户信息
     *
     * @param busCustomerInformation 客户信息
     * @return 结果
     */
    @Override
    public int insertBusCustomerInformation(BusCustomerInformation busCustomerInformation) {
        Assert.state(PhoneUtil.isMobileNumber(busCustomerInformation.getCustomerPhone()),
                "手机号格式不正确");
        List<BusCustomerInformation> busCustomerInformations =
                busCustomerInformationMapper.selectBusCustomerList();
        for (BusCustomerInformation customerInformation : busCustomerInformations) {
            Assert.state(!customerInformation.getCustomerPhone().equals(busCustomerInformation.getCustomerPhone()),
                    "手机号不唯一");
        }
        Assert.state(LicensePlateUtil.validate(busCustomerInformation.getLicensePlate()),
                "车牌号不符合要求");
        // 设置录入人
        busCustomerInformation.setCreator(SecurityUtils.getUserId().toString());
        busCustomerInformation.setCreateTime(DateUtils.getNowDate());
        return busCustomerInformationMapper.insertBusCustomerInformation(busCustomerInformation);
    }

    /**
     * 修改客户信息
     *
     * @param busCustomerInformation 客户信息
     * @return 结果
     */
    @Override
    public int updateBusCustomerInformation(BusCustomerInformation busCustomerInformation) {
        Assert.state(PhoneUtil.isMobileNumber(busCustomerInformation.getCustomerPhone()),
                "手机号格式不正确");
//        List<BusCustomerInformation> busCustomerInformations =
//                busCustomerInformationMapper.selectBusCustomerList();
//        for (BusCustomerInformation customerInformation : busCustomerInformations) {
//            Assert.state(customerInformation.getCustomerPhone().equals(busCustomerInformation.getCustomerPhone()),
//                    "手机号不唯一");
//        }
        Assert.state(LicensePlateUtil.validate(busCustomerInformation.getLicensePlate()),
                "车牌号不符合要求");
        return busCustomerInformationMapper.updateBusCustomerInformation(busCustomerInformation);
    }

    /**
     * 批量删除客户信息
     *
     * @param ids 需要删除的客户信息主键
     * @return 结果
     */
    @Override
    public int deleteBusCustomerInformationByIds(Long[] ids) {
        return busCustomerInformationMapper.deleteBusCustomerInformationByIds(ids);
    }

    /**
     * 删除客户信息信息
     *
     * @param id 客户信息主键
     * @return 结果
     */
    @Override
    public int deleteBusCustomerInformationById(Long id) {
        return busCustomerInformationMapper.deleteBusCustomerInformationById(id);
    }
}
